/*
 * Copyright (c) 2008, Jochen Mader, Tareq Doufish, Andreas Blochberger
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation 
 *   and/or other materials provided with the distribution.
 * - Neither the name of Monklypse nor the names of its contributors may be 
 *   used to endorse or promote products derived from this software without 
 *   specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
package org.monklypse.core;

import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jme.util.NanoTimer;
import com.jme.util.Timer;
import com.jmex.swt.lwjgl.LWJGLSWTCanvas;

/**
 * An implementor that should provide a starting point for your own
 * implementors.
 * 
 * @author Jochen Mader - jochen@pramari.com - Sep 10, 2008
 * 
 */
public abstract class SWTDefaultImplementor extends
		JMECanvasImplementor2<LWJGLSWTCanvas> {
	/**
	 * Scene root.
	 */
	protected Node rootNode;
	/**
	 * Timer for fps calculation.
	 */
	protected Timer timer;
	/**
	 * Average time per frame.
	 */
	protected float tpf;
	/**
	 * Camera used by this implementor.
	 */
	protected Camera cam;
	/**
	 * Initial width and height.
	 */
	protected int width, height;

	/**
	 * This class should be subclasses - not directly instantiated.
	 * 
	 * @param width
	 *            canvas width
	 * @param height
	 *            canvas height
	 */
	protected SWTDefaultImplementor(int width, int height) {
		this.width = width;
		this.height = height;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jme.system.canvas.JMECanvasImplementor#doSetup()
	 */
	@Override
	public void doSetup() {

		DisplaySystem display = DisplaySystem.getDisplaySystem();
		renderer = display.getRenderer();

		/**
		 * Create a camera specific to the DisplaySystem that works with the
		 * width and height
		 */
		cam = renderer.createCamera(width, height);

		/** Set up how our camera sees. */
		cam.setFrustumPerspective(45.0f, (float) width / (float) height, 1,
				1000);
		Vector3f loc = new Vector3f(0.0f, 0.0f, 25.0f);
		Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
		Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
		Vector3f dir = new Vector3f(0.0f, 0f, -1.0f);
		/** Move our camera to a correct place and orientation. */
		cam.setFrame(loc, left, up, dir);
		/** Signal that we've changed our camera's location/frustum. */
		cam.update();
		/** Assign the camera to this renderer. */
		renderer.setCamera(cam);

		/** Set a black background. */
		renderer.setBackgroundColor(ColorRGBA.black.clone());

		/** Get a high resolution timer for FPS updates. */
		timer = new NanoTimer();

		/** Create rootNode */
		rootNode = new Node("rootNode");

		/**
		 * Create a ZBuffer to display pixels closest to the camera above
		 * farther ones.
		 */
		ZBufferState buf = renderer.createZBufferState();
		buf.setEnabled(true);
		buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);

		rootNode.setRenderState(buf);

		simpleSetup();

		/**
		 * Update geometric and rendering information for both the rootNode and
		 * fpsNode.
		 */
		rootNode.updateGeometricState(0.0f, true);
		rootNode.updateRenderState();

		setup = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jme.system.canvas.JMECanvasImplementor#doUpdate()
	 */
	@Override
	public void doUpdate() {
		update.execute();
		timer.update();
		/** Update tpf to time per frame according to the Timer. */
		tpf = timer.getTimePerFrame();

		simpleUpdate();

		getRootNode().updateGeometricState(tpf, true);
		getRootNode().updateWorldBound();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jme.system.canvas.JMECanvasImplementor#doRender()
	 */
	@Override
	public void doRender() {
		render.execute();
		renderer.clearBuffers();
		renderer.draw(rootNode);
		simpleRender();
		renderer.displayBackBuffer();
	}

	/**
	 * Implement for custom setup.
	 */
	public abstract void simpleSetup();

	/**
	 * Implement for custom update.
	 */
	public abstract void simpleUpdate();

	/**
	 * Implement for custom render.
	 */
	public abstract void simpleRender();

	/**
	 * Get the camera for this implementor.
	 * 
	 * @return
	 */
	public Camera getCamera() {
		return cam;
	}

	/**
	 * @param cam
	 *            the cam to set
	 */
	public void setCamera(Camera cam) {
		getRenderer().setCamera(cam);
		this.cam = cam;
	}

	/**
	 * Get the root node for this implementor.
	 * 
	 * @return
	 */
	public Node getRootNode() {
		return rootNode;
	}

	/**
	 * Get the time per frame.
	 * 
	 * @return
	 */
	public float getTimePerFrame() {
		return tpf;
	}
}
